home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / complex / row.d < prev   
Encoding:
Text File  |  1991-03-10  |  3.0 KB  |  129 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program solves Poisson's equation, by using a row-at-a-time update
  3.  * algorithm. */
  4.  
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include "dino.h"
  8.  
  9. #define N 8
  10. #define P 4
  11.  
  12. environment node[P:id] {
  13.  
  14.   double d[N], s[N];
  15.  
  16.   composite setup() {
  17.  
  18.     int i;
  19.  
  20.     /* Setup d and s so that they can be used by SolveRow */
  21.     d[1] = 2;
  22.     for (i = 2; i < N - 1; i++) {
  23.       s[i] = -1 / d[i - 1];
  24.       d[i] = sqrt (4 - s[i] * s[i]);
  25.     }
  26.  
  27.   }
  28.  
  29.   void solve_row (U, i)
  30.  
  31.   double distributed U[N][N] map BlockRowOverlap;
  32.   int i;
  33.  
  34.   {
  35.     int j;
  36.     double r = 1.0;
  37.     double new[N];
  38.  
  39.     /* Perform the forward solve */
  40.     new [1] = (U[i-1][1] + U[i+1][1] + U[i][0])/d[1];
  41.     for (j=2; j<N-2; j++)
  42.       new [j] = (U[i-1][j] + U[i+1][j] - s[j]*new[j-1])/d[j];
  43.     new [N-2] = (U[i-1][N-2] +U[i+1][N-2] +U[i][N-1] -s[N-2]*new[N-3])/d[N-2];
  44.  
  45.     /* Perform the backward solve */
  46.     new [N-2] = (new[N-2])/d[N-2];
  47.     for (j=N-3; j > 0; j=j-1)
  48.       new [j] = (new[j] - s[j+1]*new[j+1])/d[j];
  49.  
  50.     /* relaxation */
  51.     for (j=1; j<N-1; j++)
  52.      U[i][j] = r*new[j] + (1-r)*U[i][j];
  53.   }
  54.  
  55.   /*  Parallel Algorithm For Row-Wise Red Black, with N/P Rows on each
  56.    * processor */
  57.  
  58.   composite solver (U)
  59.  
  60.   double distributed U[N][N] map BlockRowOverlap;
  61.  
  62.   {
  63.     /* Compute the indices of the first and last rows in each block */
  64.     int firstrow = id ? (id * N/P) : 1;
  65.     int lastrow = (id == (P-1)) ? N-2 : ((id+1) * (N/P) - 1);
  66.  
  67.     /* Compute the indices of the first even and first odd rows */
  68.     int firsteven = firstrow%2 ? firstrow + 1: firstrow ;
  69.     int firstodd = firstrow%2 ? firstrow : firstrow + 1;
  70.  
  71.     int i, k;           /* Looping variables */
  72.  
  73.     for (k=0; k<10; k++) {
  74.  
  75.       /* Update the odd rows in each block */
  76.       for (i=firstodd; i <= lastrow; i=i+2) {
  77.         if ((i==firstrow) && (i != 1) && (k != 0))
  78.           U[i-1][]#;
  79.         if ((i==lastrow) && (i != N-2) && (k != 0))
  80.           U[i+1][]#;
  81.         solve_row (U,i);
  82.         if (((i==firstrow) && (i != 1)) || ((i==lastrow) && (i != N-2)))
  83.           U[i][]# = U[i][];
  84.       }
  85.  
  86.       /* Update the even rows in each block */
  87.       for (i=firsteven; i <= lastrow; i=i+2) {
  88.         if ((i==firstrow) && (i != 1) && (k != 0))
  89.           U[i-1][]#;
  90.         if ((i==lastrow) && (i != N-2) && (k != 0))
  91.           U[i+1][]#;
  92.         solve_row (U,i);
  93.         if (((i==firstrow) && (i != 1)) || ((i==lastrow) && (i != N-2)))
  94.           U[i][]# = U[i][];
  95.       }
  96.     }
  97.   }
  98. }
  99.  
  100. environment host {
  101.  
  102.   main () {
  103.  
  104.     float Uh [N][N];
  105.     int i,j;
  106.  
  107.     /* Initialize the data */
  108.     for (i=0; i<N; i++)
  109.       for (j=0; j<N; j++)
  110.         Uh[i][j] = i*j;
  111.     for (i=1; i<N-1; i++)
  112.       for (j=1; j<N-1; j++)
  113.         Uh[i][j] = Uh[i][j] - .10;
  114.  
  115.     /* Initialize s[] and d[] */
  116.     setup ()#;
  117.  
  118.     /* Compute the results */
  119.     solver (Uh[][])#;
  120.  
  121.     /* Output the results */
  122.     for (i=0; i<N; i++) {
  123.       for (j=0; j<N; j++)
  124.         printf("%6.2f ", Uh[i][j]);
  125.       printf("\\n");
  126.     }
  127.   }
  128. }
  129.